package io.renren.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.pms.dao.GoodsInfoDao;
import io.renren.modules.pms.dao.GoodsStockDao;
import io.renren.modules.pms.dto.GoodsInfoDTO;
import io.renren.modules.pms.entity.GoodsInfoEntity;
import io.renren.modules.pms.entity.GoodsStockEntity;
import io.renren.modules.pms.entity.GoodsStockRecordEntity;
import io.renren.modules.pms.service.GoodsInfoService;
import io.renren.modules.pms.service.GoodsStockRecordService;
import io.renren.modules.pms.service.GoodsStockService;
import io.renren.modules.pms.vo.GoodsInfoVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service("goodsInfoService")
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoDao, GoodsInfoEntity> implements GoodsInfoService {


    @Resource
    private GoodsStockRecordService goodsStockRecordService;

    @Resource
    private GoodsStockService goodsStockService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String barcode = (String) params.get("barcode");
        String name = (String) params.get("name");
        String categoryName = (String) params.get("categoryName");
        Integer shelfState = (Integer) params.get("shelfState");
        IPage<GoodsInfoEntity> page = this.page(
                new Query<GoodsInfoEntity>().getPage(params),
                new QueryWrapper<GoodsInfoEntity>()
                        .like(StringUtils.isNotBlank(barcode), "barcode", barcode)
                        .like(StringUtils.isNotBlank(name), "name", name)
                        .like(StringUtils.isNotBlank(categoryName), "category_name", categoryName)
                        .eq(!Objects.isNull(shelfState), "shelf_state", shelfState)
                        .eq("delete_state", Constant.NOT_DELETE)
        );

        return new PageUtils(page);
    }

    /**
     * 查询商品详细信息
     *
     * @param id id
     * @return {@link GoodsInfoVo}
     */
    @Override
    public GoodsInfoVo getInfoById(Long id) {
        QueryWrapper<GoodsInfoEntity> wrapper = new QueryWrapper<GoodsInfoEntity>()
                .eq("id", id)
                .eq("delete_state", Constant.NOT_DELETE);
        GoodsInfoEntity goodsInfo = getOne(wrapper);
        GoodsInfoVo goodsInfoVo = ConvertUtils.sourceToTarget(goodsInfo, GoodsInfoVo.class);

        return goodsInfoVo;
    }

    /**
     * 批量删除商品
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatchIds(List<Integer> ids) {
        List<GoodsInfoEntity> goodsInfoEntities = baseMapper.selectBatchIds(ids);
        for (GoodsInfoEntity goodsInfo : goodsInfoEntities) {
            if (goodsInfo.getShelfState() == Constant.GOODS_ADDED) {
                throw new RRException("商品上架中不能删除");
            }
            if (goodsInfo.getShelfState() != Constant.GOODS_SELL_OUT) {
                throw new RRException("商品存在库存不能删除");
            }
            goodsInfo.setDeleteState(Constant.DELETE);
        }
//        批量删除商品
        updateBatchById(goodsInfoEntities);
    }

    @Transactional
    @Override
    public void updateInfo(GoodsInfoDTO goodsInfoDTO) {
        String name = goodsInfoDTO.getName();
        GoodsInfoEntity goodsInfo = ConvertUtils.sourceToTarget(goodsInfoDTO, GoodsInfoEntity.class);
        if (StringUtils.isNotBlank(name)){
//            修改商品库存表中的商品名
            GoodsStockEntity goodsStock = goodsStockService.getOne(new QueryWrapper<GoodsStockEntity>().eq("goods_id", goodsInfoDTO.getId()) );
            goodsStock.setGoodsName(name);
            goodsStockService.updateById(goodsStock);
//            修改库存出入库中的商品名
            GoodsStockRecordEntity goodsStockRecordEntity = goodsStockRecordService.getOne(new QueryWrapper<GoodsStockRecordEntity>().eq("barcode", goodsInfoDTO.getBarcode()));
            goodsStockRecordEntity.setGoodsName(name);
            goodsStockRecordService.updateById(goodsStockRecordEntity);
        }
        updateById(goodsInfo);
    }


    @Override
    public void startOrStop(Long id, Integer status) {
        GoodsInfoEntity goodsInfo = getById(id);
        goodsInfo.setShelfState(status);
        updateById(goodsInfo);
    }
}
